package wxg.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * http://tool.oschina.net/commons
 * https://github.com/j256/simplemagic
 * 
 * 
 * @author Wxg
 *
 */
public class ContentTypeAndExtension {

	public static final Extension[] EXT_ARRAY = new Extension[]{
			  new Extension(".*", "application/octet-stream")
			, new Extension(".001", "application/x-001")
			, new Extension(".301", "application/x-301")
			, new Extension(".323", "text/h323")
			, new Extension(".906", "application/x-906")
			, new Extension(".907", "drawing/907")
			, new Extension(".a11", "application/x-a11")
			, new Extension(".acp", "audio/x-mei-aac")
			, new Extension(".ai", "application/postscript")
			, new Extension(".aif", "audio/aiff")
			, new Extension(".aifc", "audio/aiff")
			, new Extension(".aiff", "audio/aiff")
			, new Extension(".anv", "application/x-anv")
			, new Extension(".asa", "text/asa")
			, new Extension(".asf", "video/x-ms-asf")
			, new Extension(".asp", "text/asp")
			, new Extension(".asx", "video/x-ms-asf")
			, new Extension(".au", "audio/basic")
			, new Extension(".avi", "video/avi")
			, new Extension(".awf", "application/vnd.adobe.workflow")
			, new Extension(".biz", "text/xml")
			, new Extension(".bmp", "application/x-bmp")
			, new Extension(".bot", "application/x-bot")
			, new Extension(".c4t", "application/x-c4t")
			, new Extension(".c90", "application/x-c90")
			, new Extension(".cal", "application/x-cals")
			, new Extension(".cat", "application/vnd.ms-pki.seccat")
			, new Extension(".cdf", "application/x-netcdf")
			, new Extension(".cdr", "application/x-cdr")
			, new Extension(".cel", "application/x-cel")
			, new Extension(".cer", "application/x-x509-ca-cert")
			, new Extension(".cg4", "application/x-g4")
			, new Extension(".cgm", "application/x-cgm")
			, new Extension(".cit", "application/x-cit")
			, new Extension(".class", "java/*")
			, new Extension(".cml", "text/xml")
			, new Extension(".cmp", "application/x-cmp")
			, new Extension(".cmx", "application/x-cmx")
			, new Extension(".cot", "application/x-cot")
			, new Extension(".crl", "application/pkix-crl")
			, new Extension(".crt", "application/x-x509-ca-cert")
			, new Extension(".csi", "application/x-csi")
			, new Extension(".css", "text/css")
			, new Extension(".cut", "application/x-cut")
			, new Extension(".dbf", "application/x-dbf")
			, new Extension(".dbm", "application/x-dbm")
			, new Extension(".dbx", "application/x-dbx")
			, new Extension(".dcd", "text/xml")
			, new Extension(".dcx", "application/x-dcx")
			, new Extension(".der", "application/x-x509-ca-cert")
			, new Extension(".dgn", "application/x-dgn")
			, new Extension(".dib", "application/x-dib")
			, new Extension(".dll", "application/x-msdownload")
			, new Extension(".doc", "application/msword")
			, new Extension(".dot", "application/msword")
			, new Extension(".drw", "application/x-drw")
			, new Extension(".dtd", "text/xml")
			, new Extension(".dwf", "Model/vnd.dwf")
			, new Extension(".dwg", "application/x-dwg")
			, new Extension(".dxb", "application/x-dxb")
			, new Extension(".dxf", "application/x-dxf")
			, new Extension(".edn", "application/vnd.adobe.edn")
			, new Extension(".emf", "application/x-emf")
			, new Extension(".eml", "message/rfc822")
			, new Extension(".ent", "text/xml")
			, new Extension(".epi", "application/x-epi")
			, new Extension(".eps", "application/x-ps")
			, new Extension(".etd", "application/x-ebx")
			, new Extension(".exe", "application/x-msdownload")
			, new Extension(".fax", "image/fax")
			, new Extension(".fdf", "application/vnd.fdf")
			, new Extension(".fif", "application/fractals")
			, new Extension(".fo", "text/xml")
			, new Extension(".frm", "application/x-frm")
			, new Extension(".g4", "application/x-g4")
			, new Extension(".gbr", "application/x-gbr")
			, new Extension(".", "application/x-")
			, new Extension(".gif", "image/gif")
			, new Extension(".gl2", "application/x-gl2")
			, new Extension(".gp4", "application/x-gp4")
			, new Extension(".hgl", "application/x-hgl")
			, new Extension(".hmr", "application/x-hmr")
			, new Extension(".hpg", "application/x-hpgl")
			, new Extension(".hpl", "application/x-hpl")
			, new Extension(".hqx", "application/mac-binhex40")
			, new Extension(".hrf", "application/x-hrf")
			, new Extension(".hta", "application/hta")
			, new Extension(".htc", "text/x-component")
			, new Extension(".htm", "text/html")
			, new Extension(".html", "text/html")
			, new Extension(".htt", "text/webviewhtml")
			, new Extension(".htx", "text/html")
			, new Extension(".icb", "application/x-icb")
			, new Extension(".ico", "image/x-icon")
			, new Extension(".iff", "application/x-iff")
			, new Extension(".ig4", "application/x-g4")
			, new Extension(".igs", "application/x-igs")
			, new Extension(".iii", "application/x-iphone")
			, new Extension(".img", "application/x-img")
			, new Extension(".ins", "application/x-internet-signup")
			, new Extension(".isp", "application/x-internet-signup")
			, new Extension(".IVF", "application/octet-stream")
			, new Extension(".java", "java/*")
			, new Extension(".jfif", "image/jpeg")
			, new Extension(".jpe", "image/jpeg")
			, new Extension(".jpeg", "image/jpeg")
			, new Extension(".jpg", "image/jpeg")
			, new Extension(".js", "application/x-javascript")
			, new Extension(".jsp", "text/html")
			, new Extension(".la1", "audio/x-liquid-file")
			, new Extension(".lar", "application/x-laplayer-reg")
			, new Extension(".latex", "application/x-latex")
			, new Extension(".lavs", "audio/x-liquid-secure")
			, new Extension(".lbm", "application/x-lbm")
			, new Extension(".lmsff", "audio/x-la-lms")
			, new Extension(".ls", "application/x-javascript")
			, new Extension(".ltr", "application/x-ltr")
			, new Extension(".m1v", "video/x-mpeg")
			, new Extension(".m2v", "video/x-mpeg")
			, new Extension(".m3u", "audio/mpegurl")
			, new Extension(".m4e", "video/mpeg4")
			, new Extension(".mac", "application/x-mac")
			, new Extension(".man", "application/x-troff-man")
			, new Extension(".math", "text/xml")
			, new Extension(".mdb", "application/msaccess")
			, new Extension(".mfp", "application/x-shockwave-flash")
			, new Extension(".mht", "message/rfc822")
			, new Extension(".mhtml", "message/rfc822")
			, new Extension(".mi", "application/x-mi")
			, new Extension(".mid", "audio/mid")
			, new Extension(".midi", "audio/mid")
			, new Extension(".mil", "application/x-mil")
			, new Extension(".mml", "text/xml")
			, new Extension(".mnd", "audio/x-musicnet-download")
			, new Extension(".mns", "audio/x-musicnet-stream")
			, new Extension(".mocha", "application/x-javascript")
			, new Extension(".movie", "video/x-sgi-movie")
			, new Extension(".mp1", "audio/mp1")
			, new Extension(".mp2", "audio/mp2")
			, new Extension(".mp2v", "video/mpeg")
			, new Extension(".mp3", "audio/mp3")
			, new Extension(".mp4", "video/mpeg4")
			, new Extension(".mpa", "video/x-mpg")
			, new Extension(".mpd", "application/vnd.ms-project")
			, new Extension(".mpe", "video/x-mpeg")
			, new Extension(".mpeg", "video/mpg")
			, new Extension(".mpg", "video/mpg")
			, new Extension(".mpga", "audio/rn-mpeg")
			, new Extension(".mpp", "application/vnd.ms-project")
			, new Extension(".mps", "video/x-mpeg")
			, new Extension(".mpt", "application/vnd.ms-project")
			, new Extension(".mpv", "video/mpg")
			, new Extension(".mpv2", "video/mpeg")
			, new Extension(".mpw", "application/vnd.ms-project")
			, new Extension(".mpx", "application/vnd.ms-project")
			, new Extension(".mtx", "text/xml")
			, new Extension(".mxp", "application/x-mmxp")
			, new Extension(".net", "image/pnetvue")
			, new Extension(".nrf", "application/x-nrf")
			, new Extension(".nws", "message/rfc822")
			, new Extension(".odc", "text/x-ms-odc")
			, new Extension(".out", "application/x-out")
			, new Extension(".p10", "application/pkcs10")
			, new Extension(".p12", "application/x-pkcs12")
			, new Extension(".p7b", "application/x-pkcs7-certificates")
			, new Extension(".p7c", "application/pkcs7-mime")
			, new Extension(".p7m", "application/pkcs7-mime")
			, new Extension(".p7r", "application/x-pkcs7-certreqresp")
			, new Extension(".p7s", "application/pkcs7-signature")
			, new Extension(".pc5", "application/x-pc5")
			, new Extension(".pci", "application/x-pci")
			, new Extension(".pcl", "application/x-pcl")
			, new Extension(".pcx", "application/x-pcx")
			, new Extension(".pdf", "application/pdf")
			, new Extension(".pdx", "application/vnd.adobe.pdx")
			, new Extension(".pfx", "application/x-pkcs12")
			, new Extension(".pgl", "application/x-pgl")
			, new Extension(".pic", "application/x-pic")
			, new Extension(".pko", "application/vnd.ms-pki.pko")
			, new Extension(".pl", "application/x-perl")
			, new Extension(".plg", "text/html")
			, new Extension(".pls", "audio/scpls")
			, new Extension(".plt", "application/x-plt")
			, new Extension(".png", "image/png")
			, new Extension(".pot", "application/vnd.ms-powerpoint")
			, new Extension(".ppa", "application/vnd.ms-powerpoint")
			, new Extension(".ppm", "application/x-ppm")
			, new Extension(".pps", "application/vnd.ms-powerpoint")
			, new Extension(".ppt", "application/vnd.ms-powerpoint")
			, new Extension(".pr", "application/x-pr")
			, new Extension(".prf", "application/pics-rules")
			, new Extension(".prn", "application/x-prn")
			, new Extension(".prt", "application/x-prt")
			, new Extension(".ps", "application/x-ps")
			, new Extension(".ptn", "application/x-ptn")
			, new Extension(".pwz", "application/vnd.ms-powerpoint")
			, new Extension(".r3t", "text/vnd.rn-realtext3d")
			, new Extension(".ra", "audio/vnd.rn-realaudio")
			, new Extension(".ram", "audio/x-pn-realaudio")
			, new Extension(".ras", "application/x-ras")
			, new Extension(".rat", "application/rat-file")
			, new Extension(".rdf", "text/xml")
			, new Extension(".rec", "application/vnd.rn-recording")
			, new Extension(".red", "application/x-red")
			, new Extension(".rgb", "application/x-rgb")
			, new Extension(".rjs", "application/vnd.rn-realsystem-rjs")
			, new Extension(".rjt", "application/vnd.rn-realsystem-rjt")
			, new Extension(".rlc", "application/x-rlc")
			, new Extension(".rle", "application/x-rle")
			, new Extension(".rm", "application/vnd.rn-realmedia")
			, new Extension(".rmf", "application/vnd.adobe.rmf")
			, new Extension(".rmi", "audio/mid")
			, new Extension(".rmj", "application/vnd.rn-realsystem-rmj")
			, new Extension(".rmm", "audio/x-pn-realaudio")
			, new Extension(".rmp", "application/vnd.rn-rn_music_package")
			, new Extension(".rms", "application/vnd.rn-realmedia-secure")
			, new Extension(".rmvb", "application/vnd.rn-realmedia-vbr")
			, new Extension(".rmx", "application/vnd.rn-realsystem-rmx")
			, new Extension(".rnx", "application/vnd.rn-realplayer")
			, new Extension(".rp", "image/vnd.rn-realpix")
			, new Extension(".rpm", "audio/x-pn-realaudio-plugin")
			, new Extension(".rsml", "application/vnd.rn-rsml")
			, new Extension(".rt", "text/vnd.rn-realtext")
			, new Extension(".rtf", "application/msword")
			, new Extension(".rv", "video/vnd.rn-realvideo")
			, new Extension(".sam", "application/x-sam")
			, new Extension(".sat", "application/x-sat")
			, new Extension(".sdp", "application/sdp")
			, new Extension(".sdw", "application/x-sdw")
			, new Extension(".sit", "application/x-stuffit")
			, new Extension(".slb", "application/x-slb")
			, new Extension(".sld", "application/x-sld")
			, new Extension(".slk", "drawing/x-slk")
			, new Extension(".smi", "application/smil")
			, new Extension(".smil", "application/smil")
			, new Extension(".smk", "application/x-smk")
			, new Extension(".snd", "audio/basic")
			, new Extension(".sol", "text/plain")
			, new Extension(".sor", "text/plain")
			, new Extension(".spc", "application/x-pkcs7-certificates")
			, new Extension(".spl", "application/futuresplash")
			, new Extension(".spp", "text/xml")
			, new Extension(".ssm", "application/streamingmedia")
			, new Extension(".sst", "application/vnd.ms-pki.certstore")
			, new Extension(".stl", "application/vnd.ms-pki.stl")
			, new Extension(".stm", "text/html")
			, new Extension(".sty", "application/x-sty")
			, new Extension(".svg", "text/xml")
			, new Extension(".swf", "application/x-shockwave-flash")
			, new Extension(".tdf", "application/x-tdf")
			, new Extension(".tg4", "application/x-tg4")
			, new Extension(".tga", "application/x-tga")
			, new Extension(".tif", "image/tiff")
			, new Extension(".tiff", "image/tiff")
			, new Extension(".tld", "text/xml")
			, new Extension(".top", "drawing/x-top")
			, new Extension(".torrent", "application/x-bittorrent")
			, new Extension(".tsd", "text/xml")
			, new Extension(".txt", "text/plain")
			, new Extension(".uin", "application/x-icq")
			, new Extension(".uls", "text/iuls")
			, new Extension(".vcf", "text/x-vcard")
			, new Extension(".vda", "application/x-vda")
			, new Extension(".vdx", "application/vnd.visio")
			, new Extension(".vml", "text/xml")
			, new Extension(".vpg", "application/x-vpeg005")
			, new Extension(".vsd", "application/vnd.visio")
			, new Extension(".vss", "application/vnd.visio")
			, new Extension(".vst", "application/vnd.visio")
			, new Extension(".vsw", "application/vnd.visio")
			, new Extension(".vsx", "application/vnd.visio")
			, new Extension(".vtx", "application/vnd.visio")
			, new Extension(".vxml", "text/xml")
			, new Extension(".wav", "audio/wav")
			, new Extension(".wax", "audio/x-ms-wax")
			, new Extension(".wb1", "application/x-wb1")
			, new Extension(".wb2", "application/x-wb2")
			, new Extension(".wb3", "application/x-wb3")
			, new Extension(".wbmp", "image/vnd.wap.wbmp")
			, new Extension(".wiz", "application/msword")
			, new Extension(".wk3", "application/x-wk3")
			, new Extension(".wk4", "application/x-wk4")
			, new Extension(".wkq", "application/x-wkq")
			, new Extension(".wks", "application/x-wks")
			, new Extension(".wm", "video/x-ms-wm")
			, new Extension(".wma", "audio/x-ms-wma")
			, new Extension(".wmd", "application/x-ms-wmd")
			, new Extension(".wmf", "application/x-wmf")
			, new Extension(".wml", "text/vnd.wap.wml")
			, new Extension(".wmv", "video/x-ms-wmv")
			, new Extension(".wmx", "video/x-ms-wmx")
			, new Extension(".wmz", "application/x-ms-wmz")
			, new Extension(".wp6", "application/x-wp6")
			, new Extension(".wpd", "application/x-wpd")
			, new Extension(".wpg", "application/x-wpg")
			, new Extension(".wpl", "application/vnd.ms-wpl")
			, new Extension(".wq1", "application/x-wq1")
			, new Extension(".wr1", "application/x-wr1")
			, new Extension(".wri", "application/x-wri")
			, new Extension(".wrk", "application/x-wrk")
			, new Extension(".ws", "application/x-ws")
			, new Extension(".ws2", "application/x-ws")
			, new Extension(".wsc", "text/scriptlet")
			, new Extension(".wsdl", "text/xml")
			, new Extension(".wvx", "video/x-ms-wvx")
			, new Extension(".xdp", "application/vnd.adobe.xdp")
			, new Extension(".xdr", "text/xml")
			, new Extension(".xfd", "application/vnd.adobe.xfd")
			, new Extension(".xfdf", "application/vnd.adobe.xfdf")
			, new Extension(".xhtml", "text/html")
			, new Extension(".xls", "application/vnd.ms-excel")
			, new Extension(".xlw", "application/x-xlw")
			, new Extension(".xml", "text/xml")
			, new Extension(".xpl", "audio/scpls")
			, new Extension(".xq", "text/xml")
			, new Extension(".xql", "text/xml")
			, new Extension(".xquery", "text/xml")
			, new Extension(".xsd", "text/xml")
			, new Extension(".xsl", "text/xml")
			, new Extension(".xslt", "text/xml")
			, new Extension(".xwd", "application/x-xwd")
			, new Extension(".x_b", "application/x-x_b")
			, new Extension(".sis", "application/vnd.symbian.install")
			, new Extension(".sisx", "application/vnd.symbian.install")
			, new Extension(".x_t", "application/x-x_t")
			, new Extension(".ipa", "application/vnd.iphone")
			, new Extension(".apk", "application/vnd.android.package-archive")
			, new Extension(".xap", "application/x-silverlight-app")
	};
	
	public static final Extension[] SPECIAL_EXT_ARRAY = new Extension[]{
			new Extension(".jpg", "image/jpeg")
	};
	
	
	public String getContentTpe(String ext){
		if(null == ext) return null;
		for(Extension ex : EXT_ARRAY){
			if(ex.name.equalsIgnoreCase(ext.trim())){
				return ex.contentType;
			}
		}
		return null;
	}
	
	public List<String> getExtensions(String contentType){
		if(null == contentType) return null;
		List<String> list = new ArrayList<String>();
		for(Extension ex : EXT_ARRAY){
			if(ex.contentType.equalsIgnoreCase(contentType.trim())){
				list.add(ex.name);
			}
		}
		return list;
	}
	
	public String getSingleExtensions(String contentType){
		if(null == contentType) return null;
		for(Extension ex : SPECIAL_EXT_ARRAY){
			if(ex.contentType.equalsIgnoreCase(contentType.trim())){
				return ex.name;
			}
		}
		for(Extension ex : EXT_ARRAY){
			if(ex.contentType.equalsIgnoreCase(contentType.trim())){
				return ex.name;
			}
		}
		return null;
	}
	
	public static class ContentType{
		public String name;
		// public Extension[] extensions;
		// public String[] extensions;
		// public Set<String> extensions;
		public String[] extensions;
		
		public ContentType(String name, String... extensions){
			this.name = name;
			this.extensions = extensions;
		}
		public ContentType(String name, Extension... extensions){
			this.name = name;
			int len = extensions.length;
			this.extensions = new String[len];
			for(int i=0;i<len;i++){
				this.extensions[i] = extensions[i].name;
			}
		}
	}
	
	public static class Extension{
		public String name;
		// public ContentType contentType;
		public String contentType;
		
		public Extension(String name, String contentType){
			this.name = name;
			this.contentType = contentType;
		}
	}
}
